package com.xmy.cultivate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.Transcript;
import com.xmy.cultivate.entity.TranscriptSet;
import com.xmy.cultivate.entity.TranscriptWritePushSet;
import com.xmy.cultivate.entity.views.ScoreData;
import com.xmy.cultivate.mapper.TranscriptMapper;
import com.xmy.cultivate.mapper.TranscriptWritePushSetMapper;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ITranscriptWritePushSetService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmy.cultivate.service.IWeiChartService;
import com.xmy.cultivate.service.QuartzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hpiggy
 * @since 2024-12-14
 */
@Service
public class TranscriptWritePushSetServiceImpl extends ServiceImpl<TranscriptWritePushSetMapper, TranscriptWritePushSet> implements ITranscriptWritePushSetService {
    @Autowired
    @Lazy
    TranscriptMapper transcriptMapper;
    @Autowired
    @Lazy
    IWeiChartService iWeiChartService;

    @Autowired
    @Lazy
    ITranscriptWritePushSetService iTranscriptWritePushSetService;

    @Autowired
    @Lazy
    IQuarterService iQuarterService;

    /**
     * 获取在读学员成绩信息
     *
     * @param yearPart        学员所在班级的年份
     * @param quarterNum      学员所在班级的学期
     * @param yearPartWrite   家长填写成绩的年份
     * @param quarterNumWrite 家长填写成功的学期
     * @param examNum         考试名称id
     * @param isStatistics    0为未填写，1为已填写
     * @return
     */
    @Override
    public List<ScoreData> getReadStudent(Integer yearPart, Integer quarterNum, Integer yearPartWrite, Integer quarterNumWrite, Integer examNum, String isStatistics, List<Integer> yearClassIdList, List<Integer> subjectsIdList) {
        QueryWrapper<Transcript> transcriptQueryWrapper = new QueryWrapper<>();
        transcriptQueryWrapper.eq(StringUtils.isNotBlank(isStatistics), "is_statistics", isStatistics);
        if (yearClassIdList.size() > 0) {
            transcriptQueryWrapper.in("year_class_id", yearClassIdList);  //年级
        }
        if (subjectsIdList.size() > 0) {
            transcriptQueryWrapper.in("subjects_id", subjectsIdList);    //科目
        }
        return transcriptMapper.getReadStudent(yearPart, quarterNum, yearPartWrite, quarterNumWrite, examNum, transcriptQueryWrapper);
    }

    /**
     * 单次推送成绩
     * @param writePushSetId
     * @param yearPart
     * @param quarterNum
     * @param examNum
     * @param isStatistics
     * @param quarter
     * @param yearClassIdList
     * @param subjectsIdList
     * @throws InterruptedException
     */
    @Override
    @Async("onlineAsync")
    public void scorePush(Long writePushSetId,Integer yearPart, Integer quarterNum, Integer examNum, String isStatistics, Quarter quarter, List<Integer> yearClassIdList,List<Integer> subjectsIdList) throws InterruptedException {

            //获取当前
            //Quarter quarterNow = iQuarterService.getNowQuarter();
            List<ScoreData> scoreDataList = this.getReadStudent(yearPart, quarterNum, yearPart, quarterNum, examNum, isStatistics, yearClassIdList, subjectsIdList);
            Integer i = 1;
            for (ScoreData scoreData : scoreDataList) {
                if (scoreData.getIsStatistics().equals(0)) {
                    iWeiChartService.scorePush(writePushSetId,scoreData.getStudentId(), quarter);
                }
            }

    }

    /**
     * 定时推送成绩
     * @param id
     * @throws InterruptedException
     */
    @Override
    @Async("onlineAsync")
    public void scoreScheduledPush(String id,LocalDate pushDate, String pushTime) throws InterruptedException {
        String isStatistics = "0";//1已填写，0未填写
        QueryWrapper<TranscriptWritePushSet> transcriptWritePushSetQueryWrapper = new QueryWrapper<>();
        transcriptWritePushSetQueryWrapper.eq("id",id);
        transcriptWritePushSetQueryWrapper.eq("status",1);
        transcriptWritePushSetQueryWrapper.eq("push_type",2);
        transcriptWritePushSetQueryWrapper.last("limit 1");
        TranscriptWritePushSet transcriptWritePushSet = iTranscriptWritePushSetService.getOne(transcriptWritePushSetQueryWrapper);
        if (transcriptWritePushSet != null){
            Long writePushSetId = transcriptWritePushSet.getId();
            Quarter quarter = iQuarterService.getQuarterForYearAndNum(transcriptWritePushSet.getYearPart(), transcriptWritePushSet.getQuarterNum());
            Integer yearPart = quarter.getYearPart();
            Integer quarterNum = quarter.getNum();
            Integer examNum = transcriptWritePushSet.getExamNum();
            String subjectsIdStr = transcriptWritePushSet.getSubjectsIdStr();
            String yearClassIdStr = transcriptWritePushSet.getYearClassIdStr();
            List<Integer> yearClassIdList = new ArrayList<>();
            List<Integer> subjectsIdList = new ArrayList<>();
            if (yearClassIdStr != null){
                String[] yearClassIds = yearClassIdStr.split(",");
                for (String s : yearClassIds) {
                    yearClassIdList.add(Integer.valueOf(s));
                }
            }
            if (subjectsIdStr != null){
                String[] subjectsIds = subjectsIdStr.split(",");
                for (String s : subjectsIds) {
                    subjectsIdList.add(Integer.valueOf(s));
                }
            }
            List<ScoreData> scoreDataList = this.getReadStudent(yearPart, quarterNum, yearPart, quarterNum, examNum, isStatistics, yearClassIdList, subjectsIdList);
            for (ScoreData scoreData : scoreDataList) {
                if (scoreData.getIsStatistics().equals(0)) {
                    iWeiChartService.scoreScheduledPush( pushTime, pushDate,writePushSetId,scoreData.getStudentId(), quarter);
                }
            }
        }

    }

}
